package 类加载器;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO:
 * <P> Date: 2022/3/29:13:50
 * <P> Description:
 */

public class GetClassLoaderTest {
    public static void main(String[] args) {
        // 获取系统类加载器
        ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
        //sun.misc.Launcher$AppClassLoader@18b4aac2
        System.out.println(systemClassLoader);

        //获取扩展类加载器
        ClassLoader extClassLoader = systemClassLoader.getParent();
        //sun.misc.Launcher$ExtClassLoader@135fbaa4
        System.out.println(extClassLoader);

        //试图获取引导类加载器 失败
        ClassLoader bootstrapClassLoader = extClassLoader.getParent();
        //null
        System.out.println(bootstrapClassLoader);

        try {
            ClassLoader classLoader = Class.forName("java.lang.String").getClassLoader();
            //null
            System.out.println(classLoader);

            ClassLoader classLoader1 = Class.forName("类加载器.GetClassLoaderTest").getClassLoader();
            //sun.misc.Launcher$AppClassLoader@18b4aac2 自定义类默认使用系统类加载器
            System.out.println(classLoader1);

            //关于数组类型的加载
            String[] strings = new String[10];
            //null 表示使用的是引导类加载器
            System.out.println(strings.getClass().getClassLoader());

            GetClassLoaderTest[] getClassLoaderTests = new  GetClassLoaderTest[10];
            //sun.misc.Launcher$AppClassLoader@18b4aac2
            System.out.println(getClassLoaderTests.getClass().getClassLoader());

            int[] ints = new int[10];
            //null 不需要类的加载器
            System.out.println(ints.getClass().getClassLoader());

        }catch (Exception e){

        }
    }
}
